package org.refresh.tests;

import java.util.List;

import org.refresh.injectors.spring.XMLSpringBeanProxyInjector;

import junit.framework.TestCase;

/*******************************************************************************
 * Copyright (c) 2005 Jules White. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Common Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors: Jules White - initial API and implementation
 ******************************************************************************/
public class XMLSpringBeanProxyInjectorTest extends TestCase {

	public void testSingleBeanProxyInjection(){
		String xml = "<beans>\r\n" + 
				" <bean id=\"foo\" class=\"org.foo.FooImpl\">\r\n" + 
				"   <property name=\"bar\" ref=\"foobar\"/>\r\n" + 
				" </bean>\r\n" + 
				"  <bean id=\"foobar\" class=\"org.foo.FooImpl\">\r\n" + 
				"   <property name=\"bar\" ref=\"foo\"/>\r\n" + 
				" </bean>\r\n" + 
				"</beans>";
		
		String shouldmatch = "<beans>\r\n" + 
				" <bean id=\"foo\" class=\"org.foo.FooImpl\">\r\n" + 
				"   <property name=\"bar\" ref=\"foobar\"/>\r\n" + 
				" </bean>\r\n" + 
				"  <bean id=\"foobar\" class=\"org.foo.FooImpl\">\r\n" + 
				"   <property name=\"bar\" ref=\"fooProxy\"/>\r\n" + 
				" </bean>\r\n" + 
				"<bean id=\"fooProxy\" class=\"test\"/></beans>";
		
		String proxycode = "<bean id=\"fooProxy\" class=\"test\"/>";
		String beanid = "foo";
		String proxyid = "fooProxy";
		XMLSpringBeanProxyInjector injector = new XMLSpringBeanProxyInjector();
		String result = injector.injectProxyViaReferenceSwapping(xml, beanid, proxyid, proxycode);
		assertEquals(shouldmatch, result);
		
	}
	
	public void testProxyAllInjection(){
		String xml = "<beans>\r\n" + 
				" <bean id=\"foo\" class=\"org.foo.FooImpl\">\r\n" + 
				"   <property name=\"bar\" ref=\"foobar\"/>\r\n" + 
				" </bean>\r\n" + 
				"  <bean id=\"foobar\" class=\"org.foo.FooImpl\">\r\n" + 
				"   <property name=\"bar\" ref=\"foo\"/>\r\n" + 
				" </bean>\r\n" + 
				"</beans>";
		
		String proxycode = "\n   <bean id=\"${proxyId}\" \r\n" + 
				"      class=\"org.springframework.aop.framework.ProxyFactoryBean\">\r\n" + 
				"      <property name=\"target\">\r\n" + 
				"         <ref local=\"${beanId}\"/>\r\n" + 
				"      </property>\r\n" + 
				"      <property name=\"interceptorNames\">\r\n" + 
				"         <list>\r\n" + 
				"            <value>refreshObserver</value>\r\n" + 
				"         </list>\r\n" + 
				"      </property>\r\n" + 
				"   </bean>\n";
		
		XMLSpringBeanProxyInjector injector = new XMLSpringBeanProxyInjector();
		String result = injector.injectProxiesForEveryBean(xml, proxycode, "delegate_of_", "");
		
//		String expected = "<beans>\r\n" + 
//				" <bean id=\"delegate_of_foo\" class=\"org.foo.FooImpl\">\r\n" + 
//				"   <property name=\"bar\" ref=\"foobar\"/>\r\n" + 
//				" </bean>\r\n" + 
//				"  <bean id=\"delegate_of_foobar\" class=\"org.foo.FooImpl\">\r\n" + 
//				"   <property name=\"bar\" ref=\"foo\"/>\r\n" + 
//				" </bean>\r\n" + 
//				"\r\n" + 
//				"   <bean id=\"foo\" \r\n" + 
//				"      class=\"org.springframework.aop.framework.ProxyFactoryBean\">\r\n" + 
//				"      <property name=\"target\">\r\n" + 
//				"         <ref local=\"delegate_of_foo\"/>\r\n" + 
//				"      </property>\r\n" + 
//				"      <property name=\"interceptorNames\">\r\n" + 
//				"         <list>\r\n" + 
//				"            <value>refreshObserver</value>\r\n" + 
//				"         </list>\r\n" + 
//				"      </property>\r\n" + 
//				"   </bean>";
//		assertEquals(expected, result);
	}
	
	public void testBeanIdExtraction(){
		String beanxml = "<beans>\r\n" + 
				" <bean id=\"foo\" class=\"org.foo.FooImpl\">\r\n" + 
				"   <property name=\"bar\" ref=\"foobar\"/>\r\n" + 
				" </bean>\r\n" + 
				"  <bean id=\"foobar\" class=\"org.foo.FooImpl\">\r\n" + 
				"   <property name=\"bar\" ref=\"foo\"/>\r\n" + 
				"   <property name=\"subfoo\">\r\n" + 
				"    <value>\r\n" + 
				"      <bean id=\"foo2\" class=\"xxx.yyy.zzz.Foo\"/>\r\n" + 
				"    </value>\r\n" + 
				"   </property>\r\n" + 
				" </bean>\r\n" + 
				"</beans>";
		XMLSpringBeanProxyInjector injector = new XMLSpringBeanProxyInjector();
		List<String> ids = injector.extractBeanIds(beanxml);
		assertEquals(3, ids.size());
		assertTrue(ids.contains("foo"));
		assertTrue(ids.contains("foobar"));
		assertTrue(ids.contains("foo2"));
	}
}
